package com.li.day.day17;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

public class Select extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Connection co=null;
        PreparedStatement pp=null;
        ResultSet rr=null;
        Object name = req.getSession().getAttribute("name3");
        String s = "select * from ppro where user_name='"+name+"'";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            co = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/p3kao", "root", "root");
             pp = co.prepareStatement(s);
             rr = pp.executeQuery();
             while (rr.next()){
                 resp.getWriter().write("账号:"+rr.getString(2)+"  ");
                 resp.getWriter().write("密码:"+rr.getString(3));
             }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            if (rr!=null){
                try {
                    rr.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (pp!=null){
                try {
                    pp.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (co!=null){
                try {
                    co.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
